package scene.composants.elements;

import java.awt.Graphics2D;
import java.awt.Point;
import java.io.Serializable;

/**
 * Objet Graphique Losange
 * Dessin d'une losange suivant un point et sa direction (angle)
 * Le losange peut etre plein ou vide
 * 
 * @author Delmas Gabriel
 */
public class Losange implements Serializable
{
	private static final long serialVersionUID = 5188889669566091955L;
	
	private int x, y;
	private double angle;
	private boolean plein;
	
	public Losange(Point p, boolean plein)
	{
		this.x = p.x;
		this.y = p.y;
		this.plein=plein;
		this.angle=Math.PI;
	}
	
	public double calculerAngle(double x1, double y1, double x2, double y2)
	{
	    double a = (x2 - x1);
	    double b = (y2 - y1);
	    double length = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
	    if (length == 0) {
	    //exeption
		}
		double p = length; //Math.sqrt(a*a+b*b);
		double theta = Math.asin(Math.abs(b) / p);
		if (b < 0) {
		  theta = -theta;
		}
		if (a < 0) {
		  theta = Math.PI - theta;
		}
		// Rescale into [0, 2*PI[.
		theta %= 2 * Math.PI;
		if (theta < 0) {
		  theta += 2 * Math.PI;
		}
		this.angle=theta;
		return theta;
    
	}

	public void dessiner(Graphics2D g2d)
	{
		g2d.translate(x, y);
		g2d.rotate(angle);
		dessinerTeteLosange(g2d);
		g2d.rotate( -angle);
		g2d.translate( -x, -y);
	}

	private void dessinerTeteLosange(Graphics2D g2d)
	{
		int x[]={-8,-16,-8,0};
		int y[]={8,0,-8,0};
		
		g2d.drawPolygon(x,y,4);
		if(plein)
			g2d.fillPolygon(x,y,4);	   
	}
}